﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StudentApartMentManage
{
    public partial class ModifyAMTInfo : Form
    {
        public ModifyAMTInfo()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sql = string.Format("select * from 公寓楼房信息表 where 公寓楼号 like '{0}%'", textBox2.Text.Trim());
            dataGridView1.DataSource = DataShow.show(sql);
        }

        private void ModifyAMTInfo_Load(object sender, EventArgs e)
        {

            dataGridView1.DataSource = DataShow.show("select * from 公寓楼房信息表");
            dataGridView2.DataSource = DataShow.show("select * from 公寓信息表");
            comboBox2.ValueMember = "管理员ID";
            comboBox2.DisplayMember = "管理员ID";
            comboBox2.DataSource = sqlHelper.GetTable("select distinct 管理员ID from 公寓管理员信息表");
            comboBox3.ValueMember = "公寓楼号";
            comboBox3.DisplayMember = "公寓楼号";
            comboBox3.DataSource = sqlHelper.GetTable("select distinct 公寓楼号 from 公寓楼房信息表");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string sql = string.Format("select * from 公寓信息表 where 公寓号 like '{0}%'", textBox1.Text.Trim());
            dataGridView2.DataSource = DataShow.show(sql);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("你真的要重置输入的信息吗？", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                textBox1.Clear();
                textBox2.Clear();
                textBox3.Clear();
                textBox4.Text = "0";
                comboBox1.Text = "0";
                radioButton1.Checked = true;
                comboBox2.Text = "";
                dateTimePicker1.Value = System.DateTime.Now;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("你真的要重置输入的信息吗？", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                textBox5.Clear();
                textBox6.Text = "5";
                textBox7.Text = "500";
                comboBox3.Text = "";
                checkBox3.Checked = false;
            }
        }

        void showinfo_1()
        {
            textBox6.Text = dataGridView1.SelectedCells[0].Value.ToString();
            textBox5.Text = dataGridView1.SelectedCells[1].Value.ToString();
            textBox3.Text = dataGridView1.SelectedCells[2].Value.ToString();
            int i = Convert.ToInt32(dataGridView1.SelectedCells[3].Value);
            if (i == 1)
                radioButton1.Checked = true;
            else
                radioButton2.Checked = true;
            textBox4.Text = dataGridView1.SelectedCells[4].Value.ToString();
            comboBox1.Text = dataGridView1.SelectedCells[5].Value.ToString();
            comboBox2.Text = dataGridView1.SelectedCells[6].Value.ToString();
            dateTimePicker1.Value = Convert.ToDateTime(dataGridView1.SelectedCells[7].Value);
        }
        void showinfo_2()
        {
            textBox9.Text = dataGridView2.SelectedCells[0].Value.ToString();
            comboBox3.Text = dataGridView2.SelectedCells[1].Value.ToString();
            textBox8.Text = dataGridView2.SelectedCells[2].Value.ToString();
            textBox7.Text = dataGridView2.SelectedCells[3].Value.ToString();
            int i = Convert.ToInt32(dataGridView2.SelectedCells[4].Value);
            if (i == 1)
            {
                checkBox3.Checked = true;
            }
            else
                checkBox3.Checked = false;

        }
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                string str = dataGridView1.SelectedCells[1].Value.ToString().Trim();
                if (MessageBox.Show("确定要修改" + str + "楼房的信息吗?", "警告", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    string sql = string.Format("update 公寓楼房信息表 set 公寓楼号='{0}',公寓楼名='{1}',公寓位置='{2}',公寓类别={3},楼层数={4},宿舍数量={5},管理员ID='{6}',启用时间='{7}' where 公寓楼号='{8}'", textBox6.Text.Trim(), textBox5.Text.Trim(), textBox3.Text.Trim(), radioButton1.Checked ? 1 : 0, Convert.ToInt32(textBox4.Text.Trim()), Convert.ToInt32(comboBox1.Text.Trim()), comboBox2.Text.Trim(), dateTimePicker1.Value.ToString(), dataGridView1.SelectedCells[0].Value);

                    int i = sqlHelper.Execute(sql);
                    if (i != -1)
                    {
                        MessageBox.Show("修改楼房信息成功!");
                        dataGridView1.DataSource = DataShow.show("select * from 公寓楼房信息表");
                    }
                    else
                        MessageBox.Show("修改楼房信息失败!");
                }
               

            }
            catch (Exception ex)
            {
                MessageBox.Show("修改失败!");
                return;
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            showinfo_1();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
                string str = dataGridView2.SelectedCells[0].Value.ToString().Trim();
                if (MessageBox.Show("确定要修改" + str + "寝室的信息吗?", "警告", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    string sql = string.Format("update 公寓信息表 set 公寓号='{0}',公寓楼号='{1}',床位数量={2},住宿费用={3},是否住满={4} where 公寓号='{5}'", textBox9.Text.Trim(), comboBox3.Text.Trim(),textBox8.Text.Trim(), textBox7.Text.Trim(), checkBox3.Checked ? 1 : 0,dataGridView2.SelectedCells[0].Value);
                    int i = sqlHelper.Execute(sql);
                    if (i != -1)
                    {
                        MessageBox.Show("修改宿舍信息成功!");
                        dataGridView2.DataSource = DataShow.show("select * from 公寓信息表");
                    }
                    else
                        MessageBox.Show("修改宿舍信息失败!");
                }


            }
            catch (Exception ex)
            {
                MessageBox.Show("修改失败!");
                return;
            }
        }

        private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            showinfo_2();
        }
    }
}
